/*
 * Copyright 2012 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.style;

import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Element;

import java.util.HashMap;
import java.util.Map;

/**
 * The helper class manipulating with css rules
 */
public class CssUtils {

    private static int counter = 0;
    private static final String CSS_CLASS_PREFIX = "nrn-";

    public static String getNextClassName() {
        counter++;
        return CSS_CLASS_PREFIX + Integer.toString(counter);
    }

    public static Rule getRule(String name) {
        StyleSheets sheets = StyleSheets.get();
        for (int i = 0; i < sheets.getLength(); i++) {
            StyleSheet sheet = sheets.get(i);
            if (sheet != null) {
                Rules rules = sheet.getRules();
                //int ruleLength = rules.getLength();
                for (int j = 0; j < rules.getLength(); j++) {
                    Rule rule = rules.get(j);
                    if (rule != null) {
                        if (name.equalsIgnoreCase(rule.getSelectorText())) {
                            return rule;
                        }
                    }
                }
            }
        }
        return null;
    }

    public static void removeRule(String name) {
        StyleSheets sheets = StyleSheets.get();
        for (int sheetIndex = 0; sheetIndex < sheets.getLength(); sheetIndex++) {
            StyleSheet sheet = sheets.get(sheetIndex);
            if (sheet != null) {
                Rules rules = sheet.getRules();
                //int ruleLength = rules.getLength();
                for (int ruleIndex = 0; ruleIndex < rules.getLength(); ruleIndex++) {
                    Rule rule = rules.get(ruleIndex);
                    if (rule != null) {
                        if (name.equalsIgnoreCase(rule.getSelectorText())) {
                            sheet.removeRule(ruleIndex);
                        }
                    }
                }
            }
        }
    }

    public static native JsArray<Element> getElementsBySelector(String selector)/*-{
        return $doc.querySelectorAll(selector);
    }-*/;


    private static final Map<String, Map<String, String>> defferedStyles = new HashMap<String, Map<String, String>>();

    public static void setStylePropertyDeffered(String selector, String styleName, String styleValue) {

        if (defferedStyles.isEmpty()) {
            Scheduler.get().scheduleFinally(new Scheduler.ScheduledCommand() {
                @Override
                public void execute() {
                    for (String selector : defferedStyles.keySet()) {
                        JsArray<Element> elements = CssUtils.getElementsBySelector(selector);
                        for (int i = 0; i < elements.length(); i++) {
                            for (Map.Entry<String, String> entry : defferedStyles.get(selector).entrySet()) {
                                elements.get(i).getStyle().setProperty(entry.getKey(), entry.getValue());
                            }
                        }
                    }
                    defferedStyles.clear();
                }
            });
        }

        if (!defferedStyles.containsKey(selector)) {
            defferedStyles.put(selector, new HashMap<String, String>());
        }
        defferedStyles.get(selector).put(styleName, styleValue);

    }
}
